package com.twg.smartUpload;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.twg.util.UploadHelper;

public class SmartDownLoadServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SmartDownLoadServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取文件存储路径
		String path = this.getServletContext().getRealPath(UploadHelper.PATH);
		//获取文件名称
		String fileName = request.getParameter("fileName");
		fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
		//处理文件名称
		String realFileName = fileName.substring(fileName.indexOf("_") + 1);
		//创建SmartUpload对象
		SmartUpload su = new SmartUpload();
		//初始化对象
		su.initialize(getServletConfig(), request, response);
		//控制浏览器下载
		su.setContentDisposition(null);
		String message = "";
		//开始下载
		try {
			su.downloadFile(path + "/" + fileName,null,new String (realFileName.getBytes("UTF-8"), "ISO-8859-1"),1024);
			message = "下载成功.";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message = "下载失败!";
			System.out.println("下载失败!");
			if(e.getMessage().indexOf("系统找不到指定的路径") != -1){  
				message = "下载失败:文件不存在！";  
            } 
			request.setAttribute("message", message);
			request.getRequestDispatcher("../Result.jsp").forward(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
